package conson.tech.manage.app.controller.backstage;


import cn.hutool.json.JSONUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import conson.tech.core.constant.SecurityConstants;
import conson.tech.core.response.Response;
import conson.tech.manage.api.app.constant.AppCommon;
import conson.tech.manage.app.constant.BusinessMessage;
import conson.tech.manage.app.entity.pojo.AppVersion;
import conson.tech.manage.app.entity.vo.AppAdvertPlaceVo;
import conson.tech.manage.app.entity.vo.AppAdvertVo;
import conson.tech.manage.app.entity.vo.PageVo;
import conson.tech.manage.app.service.AppAdvertPlaceService;
import conson.tech.manage.app.service.AppAdvertService;
import conson.tech.manage.app.service.AppVersionService;
import conson.tech.manage.common.service.CommonUploadService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.validation.Valid;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * ClassName: AdvertBackstageController
 * Description: 广告和广告位控制器
 * Author: HXL
 * Date: 2020/4/21 4:29 下午
 * Version: 1.0
 */
@Slf4j
@RestController
@Api(value = "广告和广告位控制器",tags = "广告和广告位控制器")
@RequestMapping(SecurityConstants.MANAGERAPI_MATCHER+"/")
public class AdvertBackstageController {

   @Autowired
    private AppAdvertService appAdvertService;
   @Autowired
   private AppAdvertPlaceService appAdvertPlaceService;
   @Autowired
   private CommonUploadService commonUploadService;
   @Autowired
   private AppVersionService appVersionService;

    /**
     * Title:查询广告位
     * Description:
     * @param
     * @return: conson.tech.core.response.Response<java.util.List<conson.tech.manage.app.entity.vo.AppAdvertPlaceVo>>
     * Company: GX
     * Author HXL
     * Date 2020/4/29 5:07 下午
     */
   @PostMapping("place/query")
   @ApiOperation(value = "查询广告位",notes = "查询广告位")
   //@ApiImplicitParam(name = "pageVo",value = "请求入参封装",required =true,dataType = "PageVo")
   public Response<List<AppAdvertPlaceVo>> queryAdvertPlace(){
       List<AppAdvertPlaceVo> list =  appAdvertPlaceService.queryAdvertPlace();
       return Response.success(list);
   }
    /**
     * Title: 创建广告位
     * Description:
     * @param vo
     * @return: conson.tech.core.response.Response
     * Company: GX
     * Author HXL
     * Date 2020/4/29 5:07 下午
     */
    @PostMapping("place/create")
    @ApiOperation(value = "创建广告位",notes = "创建广告位")
    @ApiImplicitParam(name = "vo",value = "请求入参封装",required =true,dataType = "AppAdvertPlaceVo")
    public Response createAdvertPlace(@Valid @RequestBody AppAdvertPlaceVo vo){
        log.info("AdvertBackstageController -> createAdvertPlace【创建广告位】入参【{}】", JSONUtil.toJsonStr(vo));
        if (vo == null || StringUtils.isEmpty(vo.getName())){
            return Response.fail("参数不完整");
        }
        appAdvertPlaceService.createAdvertPlace(vo);
        return Response.success();
    }
    /**
     * Title: 修改广告位
     * Description:
     * @param vo
     * @return: conson.tech.core.response.Response
     * Company: GX
     * Author HXL
     * Date 2020/4/29 5:07 下午
     */
    @PostMapping("place/update")
    @ApiOperation(value = "修改广告位",notes = "修改广告位")
    @ApiImplicitParam(name = "vo",value = "请求入参封装",required =true,dataType = "AppAdvertPlaceVo")
    public Response updateAdvertPlace(@Valid @RequestBody AppAdvertPlaceVo vo){
        log.info("AdvertBackstageController -> updateAdvertPlace【修改广告位】入参【{}】", JSONUtil.toJsonStr(vo));
        if (vo == null || StringUtils.isEmpty(vo.getName()) ||vo.getId() == null){
            return Response.fail("参数不完整");
        }
        appAdvertPlaceService.updateAdvertPlace(vo);
        return Response.success();
    }
    /**
     * Title: 创建广告
     * Description:
     * @param vo
     * @return: conson.tech.core.response.Response
     * Company: GX
     * Author HXL
     * Date 2020/4/29 5:08 下午
     */
    @PostMapping("advert/create")
    @ApiOperation(value = "创建广告",notes = "创建广告")
    @ApiImplicitParam(name = "vo",value = "请求入参封装",required =true,dataType = "AppAdvertVo")
    public Response createAdvert(@Valid @RequestBody AppAdvertVo vo){
        log.info("AdvertBackstageController -> createAdvert【创建广告】入参【{}】", JSONUtil.toJsonStr(vo));
        if (vo.getNavType() == 1){
            if (StringUtils.isEmpty(vo.getAppId())){
                return Response.fail(BusinessMessage.NATYPE_ERROR);
            }
        }
        if (vo.getNavType() == AppCommon.NAV_TYPE_TWO){
            if (StringUtils.isEmpty(vo.getH5TempAndroidUrl()) || StringUtils.isEmpty(vo.getH5TempIosUrl())){
                return Response.fail(BusinessMessage.NATYPE_H5_ERROR);
            }
        }
        Response response =  checkVersionValue(vo.getStartVersionValue(),vo.getEndVersionValue());
        if (response != null){
            return response;
        }
        List<AppVersion> versionList = queryVersionNum(vo.getStartVersionValue(),vo.getEndVersionValue());
        if (CollectionUtils.isEmpty(versionList)){
            return Response.fail("请传递版本值");
        }
        for (AppVersion appVersion : versionList) {
            if (appVersion.getVersionValue().intValue() == vo.getStartVersionValue().intValue()){
                vo.setStartVersion(appVersion.getVersionNum());
            }
            if (appVersion.getVersionValue().intValue() == vo.getEndVersionValue().intValue()){
                vo.setEndVersion(appVersion.getVersionNum());
            }
        }
        appAdvertService.createAdvert(vo);
        return Response.success();
    }
    private Response checkVersionValue(int startVersionValue, int endVersionValue) {
        if (startVersionValue> endVersionValue){
            return Response.fail("开始版本不能大于结束版本");
        }
        return null;
    }
    private List<AppVersion> queryVersionNum(int startVersionValue, int endVersionValue) {
        List<Integer> list = new ArrayList<>();

        if (startVersionValue != AppCommon.VERSION_VALUE && endVersionValue != AppCommon.VERSION_VALUE) {
            if (startVersionValue == endVersionValue){
                list.add(startVersionValue);
            }else {
                list.add(startVersionValue);
                list.add(endVersionValue);
            }
            return appVersionService.queryAppVersionByValue(list);
        }else if(startVersionValue == AppCommon.VERSION_VALUE && endVersionValue != AppCommon.VERSION_VALUE ){
            list.add(endVersionValue);
            List<AppVersion> versionList = appVersionService.queryAppVersionByValue(list);
            AppVersion appVersion = new AppVersion();
            appVersion.setVersionNum(AppCommon.VERSION_NAME);
            appVersion.setVersionValue(AppCommon.VERSION_VALUE);
            versionList.add(appVersion);
            return  versionList;
        }else if (startVersionValue != AppCommon.VERSION_VALUE && endVersionValue == AppCommon.VERSION_VALUE){
            list.add(startVersionValue);
            List<AppVersion> versionList = appVersionService.queryAppVersionByValue(list);
            AppVersion appVersion = new AppVersion();
            appVersion.setVersionNum(AppCommon.VERSION_NAME);
            appVersion.setVersionValue(AppCommon.VERSION_VALUE);
            versionList.add(appVersion);
            return  versionList;
        }else if (startVersionValue == AppCommon.VERSION_VALUE && endVersionValue == AppCommon.VERSION_VALUE){
            List<AppVersion> versionList = new ArrayList<>();
            AppVersion appVersion = new AppVersion();
            appVersion.setVersionNum(AppCommon.VERSION_NAME);
            appVersion.setVersionValue(AppCommon.VERSION_VALUE);
            versionList.add(appVersion);
            return versionList;
        }
        return null;
    }
    /**
     * Title: 修改广告
     * Description:
     * @param vo
     * @return: conson.tech.core.response.Response
     * Company: GX
     * Author HXL
     * Date 2020/4/30 9:33 上午
     */
    @PostMapping("advert/update")
    @ApiOperation(value = "修改广告",notes = "修改广告")
    @ApiImplicitParam(name = "vo",value = "请求入参封装",required =true,dataType = "AppAdvertVo")
    public Response updateAdvert(@Valid @RequestBody AppAdvertVo vo){
        log.info("AdvertBackstageController -> updateAdvert【修改广告】入参【{}】", JSONUtil.toJsonStr(vo));
        if (vo.getNavType() == 1){
            if (StringUtils.isEmpty(vo.getAppId())){
                return Response.fail(BusinessMessage.NATYPE_ERROR);
            }
        }
        if (vo.getNavType() == AppCommon.NAV_TYPE_TWO){
            if (StringUtils.isEmpty(vo.getH5TempAndroidUrl()) || StringUtils.isEmpty(vo.getH5TempIosUrl())){
                return Response.fail(BusinessMessage.NATYPE_H5_ERROR);
            }
        }
        Response response =  checkVersionValue(vo.getStartVersionValue(),vo.getEndVersionValue());
        if (response != null){
            return response;
        }
        List<AppVersion> versionList = queryVersionNum(vo.getStartVersionValue(),vo.getEndVersionValue());
        if (CollectionUtils.isEmpty(versionList)){
            return Response.fail("请传递版本值");
        }
        for (AppVersion appVersion : versionList) {
            if (appVersion.getVersionValue().intValue() == vo.getStartVersionValue().intValue()){
                vo.setStartVersion(appVersion.getVersionNum());
            }
            if (appVersion.getVersionValue().intValue() == vo.getEndVersionValue().intValue()){
                vo.setEndVersion(appVersion.getVersionNum());
            }
        }
        appAdvertService.updateAdvert(vo);
        return Response.success();
    }
    /**
     * Title: 删除广告
     * Description:
     * @param map
     * @return: conson.tech.core.response.Response
     * Company: GX
     * Author HXL
     * Date 2020/4/30 9:33 上午
     */
    @PostMapping("advert/delete")
    @ApiOperation(value = "根据ID删除广告",notes = "根据ID删除广告")
    @ApiImplicitParam(name = "map",value = "示例:{\"id\":1234}",required =true,dataType = "Map")
    public Response deleteAdvertById(@RequestBody Map<String,Long> map){
        if (map == null || map.get("id") == null){
            return Response.fail("请传入广告ID");
        }
        appAdvertService.deleteAdvertById(map.get("id"));
        return Response.success();
    }
    /**
     * Title: 查询广告
     * Description:
     * @param vo
     * @return: conson.tech.core.response.Response<com.baomidou.mybatisplus.extension.plugins.pagination.Page<conson.tech.manage.app.entity.vo.AppAdvertVo>>
     * Company: GX
     * Author HXL
     * Date 2020/4/30 9:33 上午
     */
    @PostMapping("advert/queryPage")
    @ApiOperation(value = "查询广告",notes = "查询广告")
    @ApiImplicitParam(name = "vo",value = "请求入参封装",required =true,dataType = "PageVo")
    public Response<Page<AppAdvertVo>> queryAdvertPage(@Valid @RequestBody PageVo vo){
        //if (vo == null || StringUtils.isEmpty(vo.getName()))
        Page<AppAdvertVo> page= appAdvertService.queryAdvertPage(vo);
        return Response.success(page);
    }

    @ApiOperation(value = "广告图片上传" ,notes = "")
    @PostMapping("advert/upload/picture")
    public Response<String> userUploadPicture(@RequestParam("file") MultipartFile file){
        if (file == null){
            return Response.fail("上传失败");
        }
        String url = commonUploadService.commonUpload(file, "/advert");
        if(StringUtils.isEmpty(url)){
            return Response.fail("上传失败");
        }
        return Response.success(url);
    }
}
